<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>function</title>
  <script type="text/javascript">
  <!--
	'use strict';
	window.alert=function(s){console.log(s);};
	function abs(x) {
		/*if (typeof x !== 'number') {
			throw 'Not a number';
		}*/
		if (x >= 0) {
			return x;
		} else {
			return -x;
		}
	}
	alert(abs(10, 'blablabla')); // 返回10
	alert(abs(-9, 'haha', 'hehe', null)); // 返回9
	alert(abs());
	/*
	function foo(x) {
		alert(x); // 10
		for (var i=0; i<arguments.length; i++) {
			alert(arguments[i]); // 10, 20, 30
		}
	}
	foo(10, 20, 30);
	*/
	function foo(a, b, ...rest) {
		console.log('a = ' + a);
		console.log('b = ' + b);
		console.log(rest);
	}
	foo(1, 2, 3, 4, 5);

	function getAge() {
		var y = new Date().getFullYear();
		return y - this.birth;
	}
	var xiaoming = {
		name: '小明',
		birth: 1990,
		age: getAge
			/*
			function () {
			*/
			/*
				var that = this; // 在方法内部一开始就捕获this
				function getAgeFromBirth() {
					var y = new Date().getFullYear();
					return y - that.birth; // 用that而不是this
				}
			*/
			/*
				function getAgeFromBirth() {
					var y = new Date().getFullYear();
					return y - this.birth; //this 指向window对象
				}
				return getAgeFromBirth();
			*/
			/*
			}
			var y = new Date().getFullYear();
			return y - this.birth; //this 指向当前对象
			*/
	};
	var fn = xiaoming.age;
	//fn(); // Uncaught TypeError: Cannot read property 'birth' of undefined
	alert(fn.apply(xiaoming));
	alert(getAge.apply(xiaoming));

	alert(xiaoming.age()); // 26

	var count = 0;
	var oldParseInt = parseInt; // 保存原函数

	window.parseInt = function () {
		count += 1;
		return oldParseInt.apply(null, arguments); // 调用原函数
	};

	// 测试:
	parseInt('10');
	parseInt('20');
	parseInt('30');
	count; // 3

  //-->
  </script>
 </head>
 <body>
  
 </body>
</html>
